import { ApiCall } from "tsrpc";
import { ReqPosition, ResPosition, Tpl_positon, client_body_position } from "../../../shared/master/word/PtlPosition";
import { player } from "../../../model/fight/body/player";
import { template } from "../../../shared/master/MsgAction";
import { npc } from "../../../model/fight/body/npc";

export default async function (call: ApiCall<ReqPosition, ResPosition>) {
    let user = call.req._player as player;
    let _cell =  user.getPosition();
    let _word = _cell.getWord();

    /**
     * 获取到上下左右的格子
     */

    let cell_top = _word.getCell(_cell.x,_cell.y + 1)
    let cell_down = _word.getCell(_cell.x,_cell.y - 1)
    let cell_right = _word.getCell(_cell.x + 1,_cell.y)
    let cell_left = _word.getCell(_cell.x - 1,_cell.y)
    user.addExp(1);
    /**
     * 读取当前格子全部单位
     */
    let bodys:client_body_position[] = [];
    const cellBodys = _cell.getBodys()
    for (let index = 0; index < cellBodys.length; index++) {
        const e = cellBodys[index];
        if(e){
            let battle_id = e.get_battle()?.id;

            if(e instanceof npc){
                bodys[index] = {
                    name:`${e.nickName}“${e.tips}”`,
                    leve:e.leve.num,
                    battle_id:battle_id,
                    uuid:e.uuid,
                    life_per:Number((e.Hp_now / e._attributes.Hp_Max).toFixed(2)),
                    type:e._bodyType
                }
            }else{
                bodys[index] = {
                    name:e.nickName,
                    leve:e.leve.num,
                    battle_id:battle_id,
                    uuid:e.uuid,
                    life_per:Number((e.Hp_now / e._attributes.Hp_Max).toFixed(2)),
                    type:e._bodyType
                }
            }
        }
    }


    let data:Tpl_positon = {
        player_cont:_word.stayPlayer.size,
        name: _word.name,
        giftBox:_cell.get_giftBox(),
        water:_cell.get_water(),
        x: _cell.x,
        y: _cell.y,
        cell_down:!!cell_down,
        cell_left:!!cell_left,
        cell_right:!!cell_right,
        cell_top:!!cell_top,
        bodys:bodys
    }
    user.sendMsg(template.Position,data)

    if(user.checkEnterCopyGame() && user.checkEnterCopyGameSend()){
        user.sendMsg(template.default,`新的轮回世界坐标已定位   确认传送发送[进入轮回空间]`)
    }
    call.succ({})
}